import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Direction, Images


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件1.烧写目标版本后，正常进入桌面-设置-系统')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        settings_pic0 = device.save_snapshot_to_local('settings0.jpeg')
        device.click(*self.app.system_item)
        system_pic0 = device.save_snapshot_to_local('system0.jpeg')
        device.refresh_layout()
        logging.info('步骤1.点击开发者选项')
        developer_item = device.get_element_by_text('开发者选项')
        device.click_element(developer_item)
        logging.info('预期结果1.进入开发者选项')
        developer_pic0 = device.save_snapshot_to_local('developer0.jpeg')
        device.refresh_layout()
        device.assert_text_exist('USB调试')
        device.assert_text_exist('显示布局边界')
        device.assert_text_exist('全局动画')
        device.assert_type_exist('Toggle')
        # device.assert_text_exist('调试GPU过度绘制')
        device.assert_text_exist('OpenHarmony兼容性测评')

        logging.info('步骤2.1开启显示布局边界')
        show_layout_switch = device.get_element_by_type('Toggle', 1)
        device.click_element(show_layout_switch)
        logging.info('预期结果2.1开启时，后退回到桌面随机打开某个应用，界面显示线条边框；关闭后刚打开的应用布局正常')
        developer_pic1 = device.save_snapshot_to_local('developer1.jpeg')
        similarity01 = Images.compare_image_similarity(developer_pic0, developer_pic1)
        assert similarity01 < 0.8, '开发者选项界面未显示边界'
        device.go_back()
        system_pic1 = device.save_snapshot_to_local('system1.jpeg')
        similarity01 = Images.compare_image_similarity(system_pic0, system_pic1)
        assert similarity01 < 0.8, '系统界面未显示边界'
        device.go_back()
        settings_pic1 = device.save_snapshot_to_local('settings1.jpeg')
        similarity01 = Images.compare_image_similarity(settings_pic0, settings_pic1)
        assert similarity01 < 0.8, '设置界面未显示边界'

        logging.info('步骤2.2 关闭显示布局边界')
        device.click(*self.app.system_item)
        device.click_element(developer_item)
        device.click_element(show_layout_switch)
        logging.info('预期结果2.2 关闭后刚打开的应用布局正常')
        developer_pic2 = device.save_snapshot_to_local('developer2.jpeg')
        similarity21 = Images.compare_image_similarity(developer_pic2, developer_pic1)
        similarity20 = Images.compare_image_similarity(developer_pic2, developer_pic0)
        assert similarity21 < 0.8, '开发者选项界面边界线条未消失'
        assert similarity20 > 0.8, '开发者选项界面边界线条未消失'
        device.go_back()
        system_pic2 = device.save_snapshot_to_local('system2.jpeg')
        similarity21 = Images.compare_image_similarity(system_pic2, system_pic1)
        similarity20 = Images.compare_image_similarity(system_pic2, system_pic0)
        assert similarity21 < 0.8, '开发者选项界面边界线条未消失'
        assert similarity20 > 0.8, '开发者选项界面边界线条未消失'
        device.go_back()
        settings_pic2 = device.save_snapshot_to_local('settings2.jpeg')
        similarity21 = Images.compare_image_similarity(settings_pic2, settings_pic1)
        similarity20 = Images.compare_image_similarity(settings_pic2, settings_pic0)
        assert similarity21 < 0.8, '开发者选项界面边界线条未消失'
        assert similarity20 > 0.8, '开发者选项界面边界线条未消失'
